/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.bean;

import com.model.LoginModel;
import java.io.IOException;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.mail.Session;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Chung
 */
@ManagedBean(name = "login")
@SessionScoped
public final class Login implements Serializable{
     private final String SLASH = "/";
    private final String USERNAME = "userName";
    private final String PASSWORD = "passwd";
    private final String HOMEPAGE = "/faces/Home.xhtml";
    private final String LOGINPAGE = "/faces/login.xhtml";
    private final String LOGINSESSION ="login";
     private final String ERRORMESSAGE ="UserName or password you entered is incorrect";
    
   
    private String username;
    private String password;
    private String fullname;
    private String error;
    private boolean rememberUser = false;
    
 

    public Login()  throws IOException  {
        
    }

    public boolean isRememberUser() {
        return rememberUser;
    }

    public void setRememberUser(boolean rememberUser) {
        this.rememberUser = rememberUser;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void action() throws IOException {
        redirect();
    }

    private void redirect() throws IOException {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        context.redirect(context.getRequestContextPath() + checkAcc());
    }

    private String checkAcc() throws IOException {
        LoginModel model = new LoginModel();
        fullname = model.loginControl(username, password);
        if (fullname != null) {
            if (rememberUser) {
                RememberMe();
            }
            saveLoginToSession();
            return HOMEPAGE;
        } else {
            error=ERRORMESSAGE;
            return LOGINPAGE;
        }
    }
   
    public void RememberMe() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Cookie btuser = new Cookie(USERNAME, username);
        Cookie btpasswd = new Cookie(PASSWORD, password);
        btuser.setMaxAge(50600);
        btpasswd.setMaxAge(50600);
        btpasswd.setPath(SLASH);
        btuser.setPath(SLASH);
        ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(btuser);
        ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(btpasswd);
        
    }
      
     private void saveLoginToSession() throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
        Login user = new Login();
        user.setFullname(fullname);
        user.setUsername(username);
        session.setAttribute(USERNAME,username);
        session.setAttribute(LOGINSESSION, user);
        
    }
}
